<?php
namespace Admin\Controller;
use Think\Controller;
class PlayController extends BaseController {
    public function playList(){
        //14110100412金永春
       $playmodel =M('play'); 
        $pageSize=5;//每页显示的数据个数
        $records=$playmodel->count(); //查询数据表中数据的总个数
        $Page = new \Think\Page($records,$pageSize);//实例化分页类，传入总数和每页显示的数量
        $list=$playmodel->order('play_id desc')->limit($Page->firstRow,$pageSize)->select();//进行分页数据查询
        // dump($list);
        // exit();
        //$comments=$commentsmodel->order('comments_id asc')->select();
        //$this->comments=$comments;
        $page1=$Page->show();//分页显示输出
        $this->assign(array('play'=>$list,'page1'=>$page1));//用数组的方式赋值数据集，赋值分页输出
        $this->display();//输出模板
    }
        public function delplay($play_id)
    {
        
        $playModel = M('play');
        $play=$playModel->where("play_id=$play_id")->find();
        unlink(realpath("Public/pic/".$play["qrpath"]));
        if($playModel->delete($play_id)) {
            $this->success('删除成功',U('playList'));//14110100412金永春
        }
        else {
            $this->error('删除失败');
        }
    }
            public function playadd($play_title,$play_url){
        
        // $code=I('post.verifycode');
        if($_FILES['file']['error']==0 ){
            $info=$this->upload();
            $name=$info["file"]["savename"];
        }
        else{
            $name="default.jpg";
        }


        $playmodel =M('play');
        $playmodel->play_title=$play_title;
        $playmodel->play_url=$play_url;
        $playmodel->play_time=date('Y-m-d H:i:s');
        $playmodel->play_pic=$name;
        $re=$playmodel->add();    
            if ($re>0)
            {
                 $this->success('发布成功',U("Play/playList"));
            }
            else
            {
                $this->error('发布错误'); 
            }
    }

    public function upload(){
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath  =     './Public/pic/'; // 设置附件上传根目录  //  
        $upload->autoSub = false;//关闭子目录，默认true
        //$upload->savePath  =     ''; // 设置附件上传(子)目录，默认目录名为年月日
        $info   =   $upload->upload();  // 上传文件 
        if(!$info) {
            $this->error($upload->getError());    
        }
        else{
            return $info;
        }                    
    }


     public function alterplay($id){
          $playmodel=M('play');
          $play=$playmodel->where("play_id=$id")->select();
          $this->play=$play;
          session('play_id',$id);
          $this->display();

    }
     public function update(){
      $id['play_id'] = I('session.play_id');
      $playmodel = M("play");
      
    
      $data['play_title'] = I('post.play_title');
      $data['play_url'] = I('play_url');
      $playmodel->play_time=date('Y-m-d H:i:s');
      if ($_FILES['file']['error'] == 0) {
          $logoinfo = $this->upload();
          $pname = $logoinfo['file']['savename'];
      } else {
          $pname = "default.jpg";
      }
        $data['play_pic'] = $pname;
        $res = $playmodel->where($id)->save($data);
        if (false !== $res){
            $this->success('修改成功',U("Play/playList"));
     
        }else{
            $this->error('信息填写有误');
        }
    }
    public function search($play_id){
        
       $playmodel =M('play');
        $play=$playmodel->where("play_id=$play_id")->select();
        $this->play=$play;
        $this->display(); 
    } 
}
?>